package com.logica.mdm.webfrontend.embedding.adapter;

import java.text.SimpleDateFormat;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.logica.mdm.webfrontend.models.party.PartySearchParams;
import com.logica.mdm.webfrontend.models.party.PartySearchResult;
import com.logica.mdm.webfrontend.utils.URLUtils;

/**
 * Oracle CRM On-Demand (OCOD) embedding adapter, for use when integrating MDM
 * Web Frontend into OCOD.
 * @author Fabien Crespel <fabien.crespel@logica.com>
 */
public class OCODEmbeddingAdapter extends AbstractEmbeddingAdapter {

	public static final String ORIGINAL_THEME_NAME = "ocod";
	public static final String ORIGINAL_SYSTEM_NAME = "CRM";
	
	protected String baseUrl;
	protected String personsHomeUrl;
	protected String personsAddUrl;
	protected String personsShowUrl;
	protected String personsEditUrl;
	
	public OCODEmbeddingAdapter() {
		setThemeName(ORIGINAL_THEME_NAME);
		setSystemName(ORIGINAL_SYSTEM_NAME);
	}
	
	@Override
	public String getPersonsHomeUrl() {
		return personsHomeUrl;
	}
	
	public void setPersonsHomeUrl(String personsHomeUrl) {
		this.personsHomeUrl = personsHomeUrl;
	}

	@Override
	public String getPersonsAddUrl() {
		return  personsAddUrl;
	}

	public void setPersonsAddUrl(String personsAddUrl) {
		this.personsAddUrl = personsAddUrl;
	}

	public String getPersonsShowUrl() {
		return  personsShowUrl;
	}

	public void setPersonsShowUrl(String personsShowUrl) {
		this.personsShowUrl = personsShowUrl;
	}

	public String getPersonsEditUrl() {
		return  personsEditUrl;
	}

	public void setPersonsEditUrl(String personsEditUrl) {
		this.personsEditUrl = personsEditUrl;
	}
	
	public String getBaseUrl() {
		return baseUrl;
	}
	
	public void setBaseUrl(String baseUrl) {
		this.baseUrl = baseUrl;
	}

	@Override
	public String getPersonsAddUrl(PartySearchParams partySearchParams) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		
		Map<String, String> query = new LinkedHashMap<String, String>();
		query.put("OMTGT", "ContactEditForm");
		query.put("OMTHD", "ContactNewNav");
		query.put("OMRET0", "ContactHomePage");
		if (!StringUtils.isEmpty(partySearchParams.getLastName()))
			query.put("ContactEditForm.Last Name", partySearchParams.getLastName());
		if (!StringUtils.isEmpty(partySearchParams.getFirstName()))
			query.put("ContactEditForm.First Name", partySearchParams.getFirstName());
		if (partySearchParams.getBirthDate() != null)
			query.put("ContactEditForm.Date of Birth", dateFormat.format(partySearchParams.getBirthDate()));
		if (!StringUtils.isEmpty(partySearchParams.getTelephoneNumber()))
			query.put("ContactEditForm.Work Phone #", partySearchParams.getTelephoneNumber());
		if (!StringUtils.isEmpty(partySearchParams.getEmailAddress()))
			query.put("ContactEditForm.Email Address", partySearchParams.getEmailAddress());
		if (!StringUtils.isEmpty(partySearchParams.getAddress()))
			query.put("ContactEditForm.Parent Alternate Address.Personal Street Address", partySearchParams.getAddress());
		if (!StringUtils.isEmpty(partySearchParams.getPostalCode()))
			query.put("ContactEditForm.Parent Alternate Address.Personal Postal Code", partySearchParams.getPostalCode());
		if (!StringUtils.isEmpty(partySearchParams.getCity()))
			query.put("ContactEditForm.Parent Alternate Address.Personal City", partySearchParams.getCity());

		return URLUtils.buildUrl(getPersonsAddUrl(), query);
	}
	
	@Override
	public String getPersonsAddUrl(PartySearchResult party) {
		if (StringUtils.isNotBlank(party.getCrmId()))
			return null;
		
		Map<String, String> query = new LinkedHashMap<String, String>();
		query.put("OMTGT", "ContactEditForm");
		query.put("OMTHD", "ContactNewNav");
		query.put("OMRET0", "ContactHomePage");
		if (!StringUtils.isEmpty(party.getLastName()))
			query.put("ContactEditForm.Last Name", party.getLastName());
		if (!StringUtils.isEmpty(party.getFirstName()))
			query.put("ContactEditForm.First Name", party.getFirstName());
		if (!StringUtils.isEmpty(party.getHomePhoneNumber()))
			query.put("ContactEditForm.Home Phone #", party.getHomePhoneNumber());
		if (!StringUtils.isEmpty(party.getWorkPhoneNumber()))
			query.put("ContactEditForm.Work Phone #", party.getWorkPhoneNumber());
		if (!StringUtils.isEmpty(party.getCellPhoneNumber()))
			query.put("ContactEditForm.Cellular Phone #", party.getCellPhoneNumber());
		if (!StringUtils.isEmpty(party.getFaxPhoneNumber()))
			query.put("ContactEditForm.Fax Phone #", party.getFaxPhoneNumber());
		if (!StringUtils.isEmpty(party.getElectronicAddress()))
			query.put("ContactEditForm.Email Address", party.getElectronicAddress());
		if (!StringUtils.isEmpty(party.getAddress()))
			query.put("ContactEditForm.Parent Alternate Address.Personal Street Address", party.getAddress());
		if (!StringUtils.isEmpty(party.getPostalCd()))
			query.put("ContactEditForm.Parent Alternate Address.Personal Postal Code", party.getPostalCd());
		if (!StringUtils.isEmpty(party.getCityName()))
			query.put("ContactEditForm.Parent Alternate Address.Personal City", party.getCityName());
		return URLUtils.buildUrl(getPersonsAddUrl(), query);
	}

	@Override
	public String getPersonsShowUrl(PartySearchResult party) {
		if (StringUtils.isBlank(party.getCrmId()))
			return null;
		
		Map<String, String> query = new LinkedHashMap<String, String>();
		query.put("OMTGT", "ContactDetailForm");
		query.put("OMTHD", "ContactDetailNav");
		query.put("OMRET0", "ContactHomePage");
		query.put("OCTYPE", "");
		query.put("ocEdit", "Y");
		if (!StringUtils.isEmpty(party.getLastName())) {
			query.put("ocTitle", party.getLastName());
			query.put("ocTitleField", "Full Name");
		}
		query.put("ContactDetailForm.Id", party.getCrmId());

		return URLUtils.buildUrl(getPersonsShowUrl(), query);
	}
	
	@Override
	public String getPersonsEditUrl(PartySearchResult party) {
		if (StringUtils.isBlank(party.getCrmId()))
			return null;
		
		Map<String, String> query = new LinkedHashMap<String, String>();
		query.put("OMTGT", "ContactEditForm");
		query.put("OMTHD", "ContactEditNav");
		query.put("OMRET0", "ContactHomePage");
		query.put("OCTYPE", "");
		query.put("ContactEditForm.Id", party.getCrmId());

		return URLUtils.buildUrl(getPersonsEditUrl(), query);
	}

}
